package cn.tedu.knows.portal.controller;

import com.sun.deploy.net.HttpResponse;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.annotation.AuthenticationPrincipal;
import org.springframework.security.core.authority.SimpleGrantedAuthority;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.servlet.resource.HttpResource;

import javax.servlet.http.HttpServletResponse;

//@Controller注解标记的控制器类中可以使用返回字符串的方式跳转页面
@Controller
public class HomeController {

    //Spring Security框架提供了判断是否拥有权限或角色的类型
    public static final GrantedAuthority STUDENT=
            new SimpleGrantedAuthority("ROLE_STUDENT");
    public static final GrantedAuthority TEACHER=
            new SimpleGrantedAuthority("ROLE_TEACHER");

    @GetMapping(value = {"/","/index.html"})
    public String index(
            @AuthenticationPrincipal UserDetails user){
        //如果是老师
        if(user.getAuthorities().contains(TEACHER)){
            return "redirect:/index_teacher.html";
        }else if(user.getAuthorities().contains(STUDENT)){
            //如果是学生
            return "redirect:/index_student.html";
        }
        // 不是老师也不是学生
        return "redirect:/login.html";
    }


}
